const response = {
  /**
   * @function 返回成功信息
   * @param {Object} res express的res
   * @param {Array|Object|String} 用户返回的信息
   */
  success: (res, data) => {
    let _obj = {
      code: 200,
      data: data
    }
    res.json(_obj)
  },
  /**
   * @function 返回错误信息
   * @param {Object} res express的res
   * @param {String} 用户返回的报错信息
   */
  error: (res, data) => {
    let _obj = {
      code: 500,
      data: data
    }
    res.json(_obj)
  },
  /**
   * @function 根据SESSION 判断用户是否登录
   * @param {Object} req express的req
   * @param {Object} res express的res
   */
  checkLogin (req, res) {
    if (!req.session.user) {
      let _obj = {
        code: 312,
        data: `请先登录`
      }
      res.json(_obj)
      return false
    } else {
      return req.session.user
    }
  },
  /**
   * @function 返回错误信息
   * @param {Object} res express的res
   * @param {Array} 用户返回的报错信息
   */
  errorList: (res, data) => {
    let _obj = {
      code: 412,
      data: data
    }
    res.json(_obj)
  },
  /**
   * @function 根据实体返回信息
   * @param {Object} res express的res
   * @param {Object} params 返回的参数
   * @param {String} entityName 实体的名字
   * @param {Boolean} isList params是否是数组
   */
  returnByEntity(res, params, entityName, isList = false) {
    let entity = require(`./entity/${entityName}`)
    if (!isList) {
      let _OBJ = this.checkByEntity(params, entity)
      this.success(res, _OBJ)
    } else {
      let _LIST = []
      params.forEach(item => {
        let _OBJ = this.checkByEntity(item, entity)
        _LIST.push(_OBJ)
      })
      this.success(res, _LIST)
    }
  },
  checkByEntity(params, entity) {
    let _OBJ = {}
    for (let i in entity) {
      let data = (params[i] === undefined || params[i] === '') ? entity[i].default === undefined ? '' : entity[i].default : params[i]
      _OBJ = {..._OBJ, ...{[i]: data}}
    }
    return _OBJ
  }
}
module.exports = response;